package com.alawn.framework.core.web.faces.utils;

import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class FacesContextUtils {

	public static ApplicationContext getApplicationContext() {
		ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext()
				.getContext();
		ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
		return context;
	}

	public static <T> T getBean(Class<T> clazz) {
		ApplicationContext context = getApplicationContext();
		return (T) context.getBean(clazz);
	}

	@SuppressWarnings("unchecked")
	public static <T> T getBean(String name) {
		ApplicationContext context = getApplicationContext();
		return (T) context.getBean(name);
	}

	public static <T> T getBean(String name, Class<T> clazz) {
		ApplicationContext context = getApplicationContext();
		return (T) context.getBean(name, clazz);
	}

}
